package com.atguigu.spzx.manager.task;

import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Component
public class OrderStatisticsTask {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;
    /**
     * 这是一个定时任务,每天两点统计前一天的订单信息,订单信息封装到OrderStatistics里面
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void orderTotalAmountStatistics(){
        //获取前一天的时间
        LocalDate localDate=LocalDate.now();
        LocalDate yesterday = localDate.minusDays(1);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String date = yesterday.format(dateTimeFormatter);
        //根据这个日期去查询到前一天的数据.封装为一个OrderStatistics对象
        OrderStatistics orderStatistics= orderInfoMapper.selectOrderStatistics(date);
        //插入OrderStatistics对象到Order_Statistics表里
        orderStatisticsMapper.insertOrderStatistics(orderStatistics);

    }
    /*
        public static void main(String[] args) {
            //获取前一天的时间
            LocalDate localDate=LocalDate.now();
            LocalDate yesterday = localDate.minusDays(1);
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String date = yesterday.format(dateTimeFormatter);
            System.out.println(date);
        }*/
}
